package com.charging.system.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.charging.common.constant.Constants;
import com.charging.common.core.domain.entity.SysUser;
import com.charging.common.utils.DateUtils;
import com.charging.system.domain.KxRechargeRecord;
import com.charging.system.service.IKxRechargeRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.charging.system.mapper.KxUserInfoMapper;
import com.charging.system.domain.KxUserInfo;
import com.charging.system.service.IKxUserInfoService;

import javax.annotation.Resource;

/**
 * 用户信息Service业务层处理
 *
 * @author charging
 * @date 2024-04-19
 */
@Service
public class KxUserInfoServiceImpl implements IKxUserInfoService
{
    @Resource
    private KxUserInfoMapper kxUserInfoMapper;

    @Resource
    private IKxRechargeRecordService kxRechargeRecordService;

    @Resource
    private IKxUserInfoService kxUserInfoService;

    /**
     * 查询用户信息
     *
     * @param userId 用户信息主键
     * @return 用户信息
     */
    @Override
    public KxUserInfo selectKxUserInfoByUserId(Long userId)
    {
        return kxUserInfoMapper.selectKxUserInfoByUserId(userId);
    }

    /**
     * 查询用户信息列表
     *
     * @param kxUserInfo 用户信息
     * @return 用户信息
     */
    @Override
    public List<KxUserInfo> selectKxUserInfoList(KxUserInfo kxUserInfo)
    {
        return kxUserInfoMapper.selectKxUserInfoList(kxUserInfo);
    }

    /**
     * 新增用户信息
     *
     * @param kxUserInfo 用户信息
     * @return 结果
     */
    @Override
    public int insertKxUserInfo(KxUserInfo kxUserInfo)
    {
        kxUserInfo.setCreateTime(DateUtils.getNowDate());
        return kxUserInfoMapper.insertKxUserInfo(kxUserInfo);
    }

    /**
     * 修改用户信息
     *
     * @param kxUserInfo 用户信息
     * @return 结果
     */
    @Override
    public int updateKxUserInfo(KxUserInfo kxUserInfo)
    {
        kxUserInfo.setUpdateTime(DateUtils.getNowDate());
        return kxUserInfoMapper.updateKxUserInfo(kxUserInfo);
    }

    /**
     * 批量删除用户信息
     *
     * @param userIds 需要删除的用户信息主键
     * @return 结果
     */
    @Override
    public int deleteKxUserInfoByUserIds(Long[] userIds)
    {
        return kxUserInfoMapper.deleteKxUserInfoByUserIds(userIds);
    }

    /**
     * 删除用户信息信息
     *
     * @param userId 用户信息主键
     * @return 结果
     */
    @Override
    public int deleteKxUserInfoByUserId(Long userId)
    {
        return kxUserInfoMapper.deleteKxUserInfoByUserId(userId);
    }

    /**
     * 更新用户余额
     * @param account
     * @return
     */
    @Override
    public int updateBalance(KxUserInfo account) {
        return kxUserInfoMapper.updateBalance(account);
    }

    @Override
    public int giveUser(Long userId, BigDecimal give) {
        KxUserInfo kxUserInfo = kxUserInfoService.selectKxUserInfoByUserId(userId);
        KxRechargeRecord kxRechargeRecord = new KxRechargeRecord();
        kxRechargeRecord.setUserId(userId);
        kxRechargeRecord.setUserName(kxUserInfo.getUserName());
        kxRechargeRecord.setTranType(Constants.TranType.NOKNOW);
        kxRechargeRecord.setTranStatus(Constants.TranStatus.YES);
        kxRechargeRecord.setRechargeSchemeId(100L);
        kxRechargeRecord.setBalance(BigDecimal.ZERO);
        kxRechargeRecord.setGive(give);
        kxRechargeRecordService.insertKxRechargeRecord(kxRechargeRecord);

        KxUserInfo newAccount = new KxUserInfo();
        newAccount.setUserId(userId);
        newAccount.setGive(kxUserInfo.getGive().add(give));
        return kxUserInfoMapper.updateKxUserInfo(newAccount);
    }
}
